# -*- coding: utf-8 -*-
import scrapy
import re
from scrapy import Request
from zufang.items import ZufangItem



class WuhanzufangSpider(scrapy.Spider):
    name = 'wuhanzufang'
    allowed_domains = ['dg.58.com']
    start_urls = ['http://dg.58.com/zufang/1/?pagetype=area&PGTID=0d300008-0019-db63-fb92-6cfe273c509b&ClickID=2']

    # start_urls = ['http://wh.58.com/hezu/34017948222125x.shtml']

    def parse(self, response):
        # print('='*20)
        # print(response.headers)

        # with open("first.html", "w") as f:
        #     f.write(response.body.decode())
        li_list = response.xpath(
            "//div[@class='mainbox']//div[@class='content']//ul[@class='listUl']//li[contains(@logr, '') and (not(@id))]")

        i = 0

        for li in li_list:
            item = ZufangItem()
            # 名称
            house_title = li.xpath("./div[@class='des']/h2/a/text()").extract_first()
            house_title = house_title.replace("\xa0 ", '').strip()
            item['house_title'] = house_title

            # 类型
            house_type_info = li.xpath("./div[@class='des']/p[@class='room']/text()").extract_first()
            house_type = house_type_info.replace(" ", "")
            if type(house_type) == type(''):
                # print(house_type)
                house_type = re.findall(r'.*(?=\s)', str(house_type))[0]
                house_type = house_type.replace('\xa0', '')
                # print(house_type)
            else:
                house_type = 'NAN'
            item['house_type'] = house_type

            # 面积
            house_area = re.findall(r"\d{1,3}㎡", house_type_info)[0]
            house_area = re.findall(r"\d{1,3}", house_area)[0]
            # print(house_area)
            try:
                house_area = int(house_area)
            except:
                pass
            item['house_area'] = house_area

            # 地址
            house_addr_1 = li.xpath("./div[@class='des']/p[@class='add']/a[1]/text()").extract_first()
            addr_1 = house_addr_1.replace("\xa0", '').replace(" ", '')
            # print(addr_1)
            house_addr_2 = li.xpath("./div[@class='des']/p[@class='add']/a[2]/text()").extract_first()
            if house_addr_2:
                addr_2 = house_addr_2.replace("\xa0", '').replace('...', '')
            else:
                addr_2 = 'NAN'
            house_addr = addr_1 + ':' + addr_2
            item['house_addr'] = house_addr

            # 详情地址
            house_href = li.xpath("./div[@class='img_list']/a/@href").extract_first()
            if house_href:
                item['house_href'] = house_href
                try:
                    phone_url = house_href
                    yield scrapy.Request(phone_url, meta={'item': item}, callback=self.parse_stock)
                except:
                    item['house_phone'] = ''
            else:
                item['house_href'] = ''
                item['house_phone'] = ''

            # 价格
            house_price = li.xpath("./div[@class='listliright']/div[@class='money']/b/text()").extract_first()
            # print(house_price)
            try:
                house_price = int(house_price)
            except:
                pass
            item['house_price'] = house_price

            # yield item

        next_url = response.xpath(
            "//div[@class='mainbox']//div[@class='content']//ul[@class='listUl']//li[contains(@id, 'bottom_ad_li')]/div[@class='pager']/a[@class='next']/@href").extract_first()
        # if next_url and i < 10:
        #     i = i + 1
        #     yield Request(url=next_url, callback=self.parse)

    def parse_stock(self, response):
        item = response.meta['item']
        # 联系电话
        house_phone = response.xpath(
            "//div[@class='house-fraud-tip']/div[@class='house-chat-phone']/span/text()").extract_first()
        item['house_phone'] = house_phone

        # 联系人
        house_agent_name = response.xpath(
            "//div[@class='house-agent-info fr']/p[@class='agent-name f16 pr']/a/text()").extract_first()
        item['house_agent_name'] = house_agent_name

        # 联系人公司
        house_agent_subgroup = response.xpath(
            "//div[@class='house-agent-info fr']/p[@class='agent-subgroup f12']/text()").extract_first()
        item['house_agent_subgroup'] = house_agent_subgroup

        # 支付方式
        house_payment_method = response.xpath(
            "//div[@class='house-pay-way f16']/span[2]/text()").extract_first()
        item['house_payment_method'] = house_payment_method

        #
        # # 名称
        # house_title = response.xpath("//div[@class='house-title']/h1/text()").extract_first()
        # item['house_title'] = house_title
        #
        house_desc_xp = response.xpath(
            "//div[@class='house-desc-item fl c_333']")

        # 租赁方式
        house_lease_type = house_desc_xp.xpath("./ul[@class='f14']/li[1]/span[2]/text()").extract_first()
        item['house_lease_type'] = house_lease_type

        # 朝向楼层
        house_floor = house_desc_xp.xpath("./ul[@class='f14']/li[3]/span[2]/text()").extract_first()
        item['house_floor'] = house_floor

        # 所在小区
        house_village = house_desc_xp.xpath("./ul[@class='f14']/li[4]/span[2]/a/text()").extract_first()
        item['house_village'] = house_village

        # 所属区域
        house_site = house_desc_xp.xpath("./ul[@class='f14']/li[5]/span[2]/a/text()").extract_first()
        item['house_site'] = house_site

        # 房源设备
        house_equipment_list = response.xpath(
            "//div[@class='main-detail-info fl']/ul[@class='house-disposal']//li")
        house_equipment = ''
        for equipment in house_equipment_list:
            if house_equipment == '':
                house_equipment = equipment.xpath("./text()").extract_first()
            else:
                house_equipment = house_equipment + ',' + equipment.xpath("./text()").extract_first()
        item['house_equipment'] = house_equipment

        # 房屋亮点
        house_bright_list = response.xpath(
            "//div[@class='house-word-introduce f16 c_555']/ul[@class='introduce-item']/li[1]/span[2]//em")
        house_bright = ''
        for house_bright_item in house_bright_list:
            if house_bright == '':
                house_bright = house_bright_item.xpath("./text()").extract_first()
            else:
                house_bright = house_bright + ',' + house_bright_item.xpath("./text()").extract_first()
        item['house_bright'] = house_bright

        # 图片详情
        house_detail_images = ''
        house_pic_list = response.xpath(
            "//*[@id='housePicList']//li")
        for house_pic_item in house_pic_list:
            if house_detail_images == '':
                house_detail_images = house_pic_item.xpath("./img/@lazy_src").extract_first()
            else:
                house_detail_images = house_detail_images + ',' + house_pic_item.xpath(
                    "./img/@lazy_src").extract_first()

        item['house_detail_images'] = house_detail_images
        item['image_urls'] = response.xpath('//*[@id="housePicList"]/li/img/@lazy_src').extract()

        # 房源描述
        house_description = response.xpath(
            "//div[@class='house-word-introduce f16 c_555']/ul[@class='introduce-item']/li[2]/span[2]/text()").extract_first()
        item['house_description'] = house_description

        # 地图位置
        house_location_href = response.xpath(
            "//div[@class='view-more-detailmap view-more']/a/@href").extract_first()
        item['house_location_href'] = house_location_href

        #
        # # 地址
        # house_addr = house_desc_xp.xpath("./ul[@class='f14']/li[6]/span[1]/text()").extract_first()
        # item['house_addr'] = house_addr
        #
        # # 价格
        # house_price = house_desc_xp.xpath("//div[@class='house-pay-way f16']/span/text()").extract_first()
        # item['house_price'] = house_price
        yield item
